package ystp.controller;


import jxl.Workbook;
import jxl.format.PageOrientation;
import jxl.format.PaperSize;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ystp.model.Kangjunyaowu;
import ystp.model.Kangjunyaowujieguo;
import ystp.model.Ysrenyuanfenpei;
import ystp.model.Ysxiangmu;
import ystp.service.*;
import ystp.support.JsonResult;
import ystp.support.PageInfo;
import ystp.support.StringHandle;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("kangjunyaowujieguo")
public class KangjunyaowujieguoController {

    @Resource
    KangjunyaowujieguoService ddService;

    @Resource
    KangjunyaowuService ddKangjunyaowuService;

    @Resource
    YsuserService ddYsuserService;

    @Resource
    YsrenyuanfenpeiService ddYsrenyuanfenpeiService;

    @Resource
    YsxiangmuService ddYsxiangmuService;

    public Logger log = LoggerFactory.getLogger(KangjunyaowujieguoController.class);


    // 导出某个委员的签字表
    @RequestMapping("qianzibiao")
    public void qianzibiao(HttpServletRequest request, HttpServletResponse response,
                           @RequestParam("xiangmuid") String ppxiangmuid,
                           @RequestParam("xiangmuleixing") String ppxiangmuleixing,
                           @RequestParam("userid") String ppuserid) {
        try {
            ddService.daochuqianzibiao(ppxiangmuid, ppuserid, request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("getKangJunResultCount")
    public JsonResult<?> getTiWaiResultCount(@RequestParam("xiangmuid") String ppxiangmuId,
                                             @RequestParam("userid") String ppuserId){
        try {
            Integer mmTongyishu=ddService.getKangJunResultCount(ppxiangmuId,ppuserId,"1");
            Integer mmButongyishu=ddService.getKangJunResultCount(ppxiangmuId,ppuserId,"0");

            Integer mmpinzhongshu=ddService.getVoitedPinzhongList(ppxiangmuId,ppuserId).size();
            Integer mmpinguishu=ddService.getKangJunResultCount(ppxiangmuId,ppuserId,"1");
            int mmjicaiyaoshu=ddService.getGuizeCount(ppuserId,ppxiangmuId,"1");//规则1为集采药
            int mmtotaljicaiyaoshu=ddKangjunyaowuService.findAllByXiangmuIdAndGuizebianhao(ppxiangmuId,"1").size();

            int mmfenleishu=ddService.findAgreedFenleiList(ppxiangmuId,ppuserId).size();
            //int mmtotalfenleishu=ddKangjunyaowuService.findFeileiByXiangmuId(ppxiangmuId).size();
           // List<Map<String, Object>> mmFeileiList=ddKangjunyaowuService.findFeileiByXiangmuId(ppxiangmuId);


            Integer mmTotal=ddKangjunyaowuService.getVoteCount(ppxiangmuId);
            Map<String, Object> mmMap = new HashMap<String, Object>();
            mmMap.put("pinzhongshu", mmpinzhongshu);
            mmMap.put("fenleishu", mmfenleishu);
            mmMap.put("pinguishu", mmpinguishu);
            mmMap.put("jicaiyaoshu", mmjicaiyaoshu);
            mmMap.put("shengyujicaiyaoshu", mmtotaljicaiyaoshu-mmjicaiyaoshu);
            return JsonResult.getSuccessResult(mmMap);
        }catch (Exception e) {
            e.printStackTrace();
            log.error("kangjunyaowujieguo/getKangJunResultCount:error",e);
            return JsonResult.getErrorResult("kangjunyaowujieguo/getKangJunResultCount:error " + e.getMessage());
        }
    }

    @RequestMapping("toupiao")
    public JsonResult<?> toupiao(@RequestParam("id") String ppdetailId,
                                 @RequestParam("pinzhong") int ppPinzhong,
                                 @RequestParam("pingui") String ppPingui,
                                 @RequestParam("userid") String ppuserId,
                                 @RequestParam("result") String ppresult){
        try {
            Kangjunyaowujieguo mmKangjunyaowujieguo=new Kangjunyaowujieguo();
            Kangjunyaowu mmKangjunyaowu=ddKangjunyaowuService.findOne(ppdetailId);

            Ysxiangmu mmYsxiangmu=ddYsxiangmuService.findOne(mmKangjunyaowu.getXiangmuid());
            if(mmYsxiangmu.getIsactived()!=1){
                return JsonResult.getErrorResult("表决失败，投票尚未启动");
            }

            List<Ysrenyuanfenpei> listfenpei = ddYsrenyuanfenpeiService.findbyUserIDandXiangmuId(ppuserId,mmKangjunyaowu.getXiangmuid());
            if(listfenpei.get(0).getIssubmit()==1){
                return JsonResult.getErrorResult("表决失败，当前投票项已提交");
            }



            if("1".equals(ppresult)) {
                String mmGuizebianhao=mmKangjunyaowu.getGuizebianhao();
                String mmZiguizebianhao=mmKangjunyaowu.getBeizhu3();

                Integer mmpinzhongshu=ddService.getVoitedPinzhongList(mmKangjunyaowu.getXiangmuid(),ppuserId).size();
                if(mmpinzhongshu==48){
                    if((ddService.getResultByPinzhongAndXiangmuId(String.valueOf(ppPinzhong),mmKangjunyaowu.getXiangmuid(),ppuserId).size()==0)
                    &&ppPinzhong!=30&&ppPinzhong!=31){
                        return JsonResult.getErrorResult("表决失败，勾选品种数超过48");
                    }
                }

                if(ddService.getKangJunResultCount(mmKangjunyaowu.getXiangmuid(),ppuserId,"1")==79){
                    return JsonResult.getErrorResult("表决失败，勾选品规数超过79");
                }


                if(mmGuizebianhao!=null&&!"".equals(mmGuizebianhao)){//有规则限制
                    int mmXianzhishu=Integer.valueOf(mmKangjunyaowu.getXianzhishu());
                    String mmXiangmuId=mmKangjunyaowu.getXiangmuid();
                    int mmvotedagreedcount=ddService.getGuizeCount(ppuserId,mmXiangmuId,mmGuizebianhao);
                    if("0".equals(mmGuizebianhao)){
                        return JsonResult.getErrorResult("表决失败，此药品需淘汰");
                    }
                    if("3".equals(mmGuizebianhao)&&mmvotedagreedcount>=mmXianzhishu){
                        return JsonResult.getErrorResult("表决失败，品规4、10、11、12、18最多5选4");
                    }
                    if("4".equals(mmGuizebianhao)&&mmvotedagreedcount>=mmXianzhishu){
                        return JsonResult.getErrorResult("表决失败，品规23、24、25、26最多4选3");
                    }
                    if("5".equals(mmGuizebianhao)&&mmvotedagreedcount>=mmXianzhishu){
                        return JsonResult.getErrorResult("表决失败，品规19、20、21、22最多4选2");
                    }
                    if("6".equals(mmGuizebianhao)&&mmvotedagreedcount>=mmXianzhishu){
                        return JsonResult.getErrorResult("表决失败，品规27、31最多2选1");
                    }
                    if("7".equals(mmGuizebianhao)&&mmvotedagreedcount>=mmXianzhishu){
                        return JsonResult.getErrorResult("表决失败7，超过限制数");
                    }
                    if("8".equals(mmGuizebianhao)&&mmvotedagreedcount>=mmXianzhishu){
                        return JsonResult.getErrorResult("表决失败，品规37、38、39最多3选2");
                    }
                    if("9".equals(mmGuizebianhao)&&mmvotedagreedcount>=mmXianzhishu){
                        return JsonResult.getErrorResult("表决失败9，超过限制数");
                    }
                    if("10".equals(mmGuizebianhao)&&mmvotedagreedcount>=mmXianzhishu){
                        return JsonResult.getErrorResult("表决失败10，超过限制数");
                    }
                    if("11".equals(mmGuizebianhao)&&mmvotedagreedcount>=mmXianzhishu){
                        return JsonResult.getErrorResult("表决失败，品规63、64、65最多3选2");
                    }
                    if("12".equals(mmGuizebianhao)&&mmvotedagreedcount>=mmXianzhishu){
                        return JsonResult.getErrorResult("表决失败，品规79、80、81超过一品两规");
                    }
                    if("13".equals(mmGuizebianhao)&&mmvotedagreedcount>=mmXianzhishu){
                        return JsonResult.getErrorResult("表决失败13，超过限制数");
                    }
                    if("14".equals(mmGuizebianhao)&&mmvotedagreedcount>=mmXianzhishu){
                        return JsonResult.getErrorResult("表决失败14，超过限制数");
                    }
                    if("15".equals(mmGuizebianhao)&&mmvotedagreedcount>=mmXianzhishu){
                        return JsonResult.getErrorResult("表决失败15，超过限制数");
                    }
                    if("99".equals(mmGuizebianhao)){
                        return JsonResult.getErrorResult("表决失败，品规51、52、53中集采必选，不能超过一品两规");
                    }


                    if(mmZiguizebianhao!=null&&!"".equals(mmZiguizebianhao)) {//有子规则限制
                        int mmZixianzhishu=Integer.valueOf(mmKangjunyaowu.getBeizhu2());
                        int mmvotedagreedcount2=ddService.getZiGuizeCount(ppuserId,mmXiangmuId,mmZiguizebianhao);
                        if(mmvotedagreedcount2>=mmZixianzhishu){
                            return JsonResult.getErrorResult("表决失败"+mmZiguizebianhao+"，超过限制数");
                        }

                    }



                        if (ddService.checkexistByUserId(ppPinzhong, ppPingui, ppuserId) != null) {
                            String mmId = ddService.checkexistByUserId(ppPinzhong, ppPingui, ppuserId).getId();
                            mmKangjunyaowujieguo.setId(mmId);
                            mmKangjunyaowujieguo.setResult(ppresult);
                            mmKangjunyaowujieguo.setCreatetime(new Date());
                            return ddService.modify(mmKangjunyaowujieguo) == true ? JsonResult.getSuccessResult("表决成功")
                                    : JsonResult.getErrorResult("表决失败");
                        }else {
                            mmKangjunyaowujieguo.setId(UUID.randomUUID().toString());
                            mmKangjunyaowujieguo.setDetailid(ppdetailId);
                            mmKangjunyaowujieguo.setPinzhong(ppPinzhong);
                            mmKangjunyaowujieguo.setPingui(ppPingui);
                            mmKangjunyaowujieguo.setUserid(ppuserId);
                            mmKangjunyaowujieguo.setResult(ppresult);
                            mmKangjunyaowujieguo.setCreatetime(new Date());
                            return ddService.add(mmKangjunyaowujieguo) == true ? JsonResult.getSuccessResult("表决成功")
                                    : JsonResult.getErrorResult("表决失败");
                        }

                }else {
                    if (ddService.checkexistByUserId(ppPinzhong, ppPingui, ppuserId) != null) {
                        String mmId = ddService.checkexistByUserId(ppPinzhong, ppPingui, ppuserId).getId();
                        mmKangjunyaowujieguo.setId(mmId);
                        mmKangjunyaowujieguo.setResult(ppresult);
                        mmKangjunyaowujieguo.setCreatetime(new Date());
                        return ddService.modify(mmKangjunyaowujieguo) == true ? JsonResult.getSuccessResult("表决成功")
                                : JsonResult.getErrorResult("表决失败");
                    }else {
                        mmKangjunyaowujieguo.setId(UUID.randomUUID().toString());
                        mmKangjunyaowujieguo.setDetailid(ppdetailId);
                        mmKangjunyaowujieguo.setPinzhong(ppPinzhong);
                        mmKangjunyaowujieguo.setPingui(ppPingui);
                        mmKangjunyaowujieguo.setUserid(ppuserId);
                        mmKangjunyaowujieguo.setResult(ppresult);
                        mmKangjunyaowujieguo.setCreatetime(new Date());
                        return ddService.add(mmKangjunyaowujieguo) == true ? JsonResult.getSuccessResult("表决成功")
                                : JsonResult.getErrorResult("表决失败");
                    }
                }

            }else{
                if (ddService.checkexistByUserId(ppPinzhong, ppPingui, ppuserId) != null) {
                    String mmId = ddService.checkexistByUserId(ppPinzhong, ppPingui, ppuserId).getId();
                    mmKangjunyaowujieguo.setId(mmId);
                    mmKangjunyaowujieguo.setResult(ppresult);
                    mmKangjunyaowujieguo.setCreatetime(new Date());
                    return ddService.modify(mmKangjunyaowujieguo) == true ? JsonResult.getSuccessResult("表决成功")
                            : JsonResult.getErrorResult("表决失败");
                }else {
                    mmKangjunyaowujieguo.setId(UUID.randomUUID().toString());
                    mmKangjunyaowujieguo.setDetailid(ppdetailId);
                    mmKangjunyaowujieguo.setPinzhong(ppPinzhong);
                    mmKangjunyaowujieguo.setPingui(ppPingui);
                    mmKangjunyaowujieguo.setUserid(ppuserId);
                    mmKangjunyaowujieguo.setResult(ppresult);
                    mmKangjunyaowujieguo.setCreatetime(new Date());
                    return ddService.add(mmKangjunyaowujieguo) == true ? JsonResult.getSuccessResult("表决成功")
                            : JsonResult.getErrorResult("表决失败");
                }
            }


        }catch (Exception e) {
            e.printStackTrace();
            log.error("kangjunyaowujieguo/toupiao:error",e);
            return JsonResult.getErrorResult("kangjunyaowujieguo/toupiao:error " + e.getMessage());
        }
    }

    @RequestMapping("votesure")
    public JsonResult<?> votesure(@RequestParam("xiangmuid") String ppxiangmuid,
                                  @RequestParam("userid") String ppuserId){

        try {

            Ysxiangmu mmYsxiangmu=ddYsxiangmuService.findOne(ppxiangmuid);
            if(mmYsxiangmu.getIsactived()!=1){
                return JsonResult.getErrorResult("表决失败，投票尚未启动");
            }


            List<Kangjunyaowu> mmList=ddKangjunyaowuService.findAllByXiangmuIdAndGuizebianhao(ppxiangmuid,"1");
            for(Kangjunyaowu mmKangjunyaowu :mmList){
                String mmDetailId=mmKangjunyaowu.getId();
                List<Kangjunyaowujieguo> mmKangjunyaowujieguoList=ddService.findByDetailIdAndUserId(mmDetailId,ppuserId);
                if(mmKangjunyaowujieguoList.size()>0){
                    String mmId=mmKangjunyaowujieguoList.get(0).getId();
                    Kangjunyaowujieguo mmKangjunyaowujieguo=ddService.findone(mmId);
                    mmKangjunyaowujieguo.setResult("1");
                     ddService.modify(mmKangjunyaowujieguo);
                }else {
                    Kangjunyaowujieguo mmKangjunyaowujieguo=new Kangjunyaowujieguo();
                    String mmId=UUID.randomUUID().toString();
                    mmKangjunyaowujieguo.setId(mmId);
                    mmKangjunyaowujieguo.setDetailid(mmDetailId);
                    mmKangjunyaowujieguo.setUserid(ppuserId);
                    mmKangjunyaowujieguo.setResult("1");
                    mmKangjunyaowujieguo.setCreatetime(new Date());
                    mmKangjunyaowujieguo.setPinzhong(mmKangjunyaowu.getPinzhong());
                    mmKangjunyaowujieguo.setPingui(mmKangjunyaowu.getPingui());
                    ddService.add(mmKangjunyaowujieguo);
                }
            }

            return  JsonResult.getSuccessResult("表决成功");

        }catch (Exception e) {
            e.printStackTrace();
            log.error("kangjunyaowujieguo/votesure:error",e);
            return JsonResult.getErrorResult("kangjunyaowujieguo/votesure:error " + e.getMessage());
        }
    }


    @RequestMapping("findHuizongbiaolist")
    public JsonResult<?> findHuizongbiaolist(@RequestParam("xiangmuid") String ppxiangmuid){
        try {
            List<Map<String,Object>> mmHuizongbiaoList = new ArrayList<>();
            List<Ysrenyuanfenpei> mmUserList=ddYsrenyuanfenpeiService.findUserByXiangmuId(ppxiangmuid);
            Double mm=((double)2/(double)3)*mmUserList.size();
            int mmTongguopiaoshu=(int)Math.ceil(mm);

            List<Map<String,Object>> mmPinzhongpaixuList=ddService.getPinzhongpaixu(ppxiangmuid);
            int a=0;
            for (Map<String, Object> mmPinzhongpaixuMap : mmPinzhongpaixuList) {
                a=a+1;
                String mmPinzhong = mmPinzhongpaixuMap.get("pinzhong").toString();
                int mmPinzhongpiaoshu = 0;
                if (mmPinzhongpaixuMap.get("piaoshu") != null && !"".equals(mmPinzhongpaixuMap.get("piaoshu").toString())) {
                    mmPinzhongpiaoshu = Integer.valueOf(mmPinzhongpaixuMap.get("piaoshu").toString());
                }

                int mmPinzhongHebingshu = 0;

                List<Map<String, Object>> mmPinguiList = ddService.getAgreedPinguiListByPinzhong(ppxiangmuid, mmPinzhong);
                if (mmPinguiList.size()>0){
                    mmPinzhongHebingshu = mmPinguiList.size();
                }else{
                    mmPinzhongHebingshu = mmPinzhongHebingshu+1;
                }
                int pg=0;

                for (Map<String, Object> mmPinguiMap : mmPinguiList) {
                    pg=pg+1;
                    String mmPingui = mmPinguiMap.get("pingui").toString();
                    int mmPinguipiaoshu = 0;

                    if(mmPinguiMap.get("piaoshu")==null){
                        mmPinguipiaoshu=0;
                    }else {
                        mmPinguipiaoshu=Integer.valueOf(mmPinguiMap.get("piaoshu").toString());
                    }
                    String mmPinzhongtext = String.valueOf(mmPinzhongpiaoshu) + "/" + String.valueOf(mmUserList.size());
                    String mmPinguitext = String.valueOf(mmPinguipiaoshu) + "/" + String.valueOf(mmUserList.size());
                    mmPinguiMap.put("pinzhongtext",mmPinzhongtext);
                    mmPinguiMap.put("pinguitext",mmPinguitext);

                    String mmJieguotext="";


                    if(mmPinguipiaoshu>=mmTongguopiaoshu){
//                        if(2>=mmPinguiList.size()){
//                            mmJieguotext="通过";
//                        }
//                        if(mmPinguiList.size()>2&&2>=pg){
//                            mmJieguotext="通过";
//                        }
//                        if(mmPinguiList.size()>2&&pg>2){
//                            mmJieguotext="不通过";
//                        }
                        mmJieguotext="通过";
                    }else {
                        mmJieguotext="不通过";
                    }


//                    if(a>48){
//                        mmJieguotext="不通过";
//                    }

                    mmPinguiMap.put("jieguotext",mmJieguotext);


                    //mmPinguiMap.put("pinzhonghebinghang",mmPinzhongHebingshu);
                }
                mmPinzhongpaixuMap.put("pinguilist",mmPinguiList);
                mmPinzhongpaixuMap.put("pinzhonghebinghang",mmPinzhongHebingshu);

                mmHuizongbiaoList.add(mmPinzhongpaixuMap);
            }
            Map<String, Object> mmReturnMap = new HashMap<String, Object>();
            mmReturnMap.put("HuizongbiaoList", mmHuizongbiaoList);

            return JsonResult.getSuccessResult(mmReturnMap);

        }catch (Exception e) {
            e.printStackTrace();
            log.error("kangjunyaowujieguo/findHuizongbiaolist:error",e);
            return JsonResult.getErrorResult("kangjunyaowujieguo/findHuizongbiaolist:error " + e.getMessage());
        }
    }
    @RequestMapping("huizongbiao")
    public void huizongbiao(HttpServletRequest request, HttpServletResponse response,
                            @RequestParam("xiangmuid") String ppxiangmuid,
                            @RequestParam("xiangmuleixing") String ppxiangmuleixing){
        List<Ysrenyuanfenpei> mmUserList=ddYsrenyuanfenpeiService.findUserByXiangmuId(ppxiangmuid);
        List<Map<String,Object>> mmPinzhongpaixuList=ddService.getPinzhongpaixu(ppxiangmuid);

        Double mm=((double)2/(double)3)*mmUserList.size();
        int mmTongguopiaoshu=(int)Math.ceil(mm);

        response.reset();
        response.setContentType("application/x-download;charset=utf-8");
        OutputStream os=null;
        WritableWorkbook wbook=null;

        try {
            os = new BufferedOutputStream(response.getOutputStream());//得到输出流的语句
            //response.getOutputStream();// 取得输出流
            response.reset();// 清空输出流
            SimpleDateFormat mmDateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
            String wenjiantitle = "抗菌药物目录结果汇总表" + StringHandle.tiemString(); //文件名
            String tmptitle = "东部战区总医院抗菌药品目录意见投票表"; // 标题
            response.setHeader("Content-disposition", "attachment; filename=" + new String(wenjiantitle.getBytes(), "iso8859-1") + ".xls");// 设定输出文件头
            response.setContentType("application/vnd.ms-excel");// 定义输出类型
            wbook = Workbook.createWorkbook(os); // 建立excel文件
            WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称
            // 设置excel标题
            WritableFont wfont = new WritableFont(WritableFont.ARIAL, 20, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); //设置表格样式

            WritableCellFormat wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN); // 添加边框
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            //wcfFC.setBackground(Colour.AQUA);
            Label label = new Label(0, 0, tmptitle, wcfFC);
            WritableCellFormat cellFormat = new WritableCellFormat();
            cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
            cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            cellFormat.setWrap(true);
            //cellFormat.setBackground(Colour.AQUA);
            cellFormat.setFont(wfont);
            label.setCellFormat(cellFormat);
            wsheet.addCell(label);
            wsheet.addCell(new Label(0, 0, tmptitle, wcfFC));
            /*wsheet.addCell(new Label(7, 0, tmptitle, wcfFC));*/
            wsheet.setRowView(0,800); //第一行高度
            wsheet.setRowView(1,600); //第二行高度
            wsheet.mergeCells(0, 0, 20, 0);  //合并单元格(第一列的第一行和第十三列的第一行合并)
            wsheet.setColumnView(0,5); //宽度
            wsheet.setColumnView(1,5); //宽度
            wsheet.setColumnView(2,5); //宽度
            wsheet.setColumnView(3,10); //宽度
            wsheet.setColumnView(4,10); //宽度
            wsheet.setColumnView(5,10); //宽度
            wsheet.setColumnView(6,10); //宽度
            wsheet.setColumnView(7,10); //宽度
            wsheet.setColumnView(8,5); //宽度
            wsheet.setColumnView(9,5); //宽度
            wsheet.setColumnView(10,5); //宽度
            wsheet.setColumnView(11,5); //宽度
            wsheet.setColumnView(12,10); //宽度
            wsheet.setColumnView(13,10); //宽度
            wsheet.setColumnView(14,10); //宽度
            wsheet.setColumnView(15,10); //宽度
            wsheet.setColumnView(16,10); //宽度
            wsheet.setColumnView(17,10); //宽度
            wsheet.setColumnView(18,10); //宽度
            wsheet.setColumnView(19,10); //宽度
            wsheet.setColumnView(20,10); //宽度
            // 开始生成主体内容
            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);

            wsheet.addCell(new Label(0, 1, "排名",wcfFC));
            wsheet.mergeCells(0, 1, 0, 2);

            wsheet.addCell(new Label(1, 1, "品种",wcfFC));
            wsheet.mergeCells(1, 1, 1, 2);

            wsheet.addCell(new Label(2, 1, "品规",wcfFC));
            wsheet.mergeCells(2, 1, 2, 2);

            wsheet.addCell(new Label(3, 1, "药品名称",wcfFC));
            wsheet.mergeCells(3, 1, 3, 2);

            wsheet.addCell(new Label(4, 1, "分类",wcfFC));
            wsheet.mergeCells(4, 1, 4, 2);

            wsheet.addCell(new Label(5, 1, "规格",wcfFC));
            wsheet.mergeCells(5, 1, 5, 2);

            wsheet.addCell(new Label(6, 1, "单位",wcfFC));
            wsheet.mergeCells(6, 1, 6, 2);

            wsheet.addCell(new Label(7, 1, "医保类别",wcfFC));
            wsheet.mergeCells(7, 1, 7, 2);

            wsheet.addCell(new Label(8, 1, "推荐依据",wcfFC));
            wsheet.mergeCells(8, 1, 11, 1);

            wsheet.addCell(new Label(12, 1, "所在目录",wcfFC));
            wsheet.mergeCells(12, 1, 13, 1);

            wsheet.addCell(new Label(14, 1, "半年无采购记录",wcfFC));
            wsheet.mergeCells(14, 1, 15, 1);

            wsheet.addCell(new Label(16, 1, "抗菌药物管理工作小组预选意见",wcfFC));
            wsheet.mergeCells(16, 1, 17, 1);

            wsheet.addCell(new Label(18, 1, "委员表决票数",wcfFC));
            wsheet.mergeCells(18, 1, 19, 1);
          /*  wsheet.addCell(new Label(18, 1, "品种同意数/总票数",wcfFC));
            wsheet.mergeCells(18, 1, 18, 2);

            wsheet.addCell(new Label(19, 1, "品规同意数/总票数",wcfFC));
            wsheet.mergeCells(19, 1, 19, 2);*/

            wsheet.addCell(new Label(20, 1, "表决结果",wcfFC));
            wsheet.mergeCells(20, 1, 20, 2);

            // 第四行内容
            wsheet.addCell(new Label(8, 2, "基药",wcfFC));
            wsheet.addCell(new Label(9, 2, "集采药",wcfFC));
            wsheet.addCell(new Label(10, 2, "一致性评价",wcfFC));
            wsheet.addCell(new Label(11, 2, "15版指导原则",wcfFC));
            wsheet.addCell(new Label(12, 2, "院本部",wcfFC));
            wsheet.addCell(new Label(13, 2, "秦淮",wcfFC));
            wsheet.addCell(new Label(14, 2, "院本部",wcfFC));
            wsheet.addCell(new Label(15, 2, "秦淮",wcfFC));
            wsheet.addCell(new Label(16, 2, "是否为前48个品种",wcfFC));
            wsheet.addCell(new Label(17, 2, "品规得票（总票数25）",wcfFC));
            wsheet.addCell(new Label(18, 2, "品种同意数/总票数",wcfFC));
            wsheet.addCell(new Label(19, 2, "品规同意数/总票数",wcfFC));

            // 表头结束

            wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);

            int count = mmPinzhongpaixuList.size();
            int mmpinzhongKaishihang = 3;
            int mmpinpaiKaishihang = 3;


            int a=1;

            for (Map<String, Object> mmPinzhongpaixuMap : mmPinzhongpaixuList) {
                if(48>=a){
                    wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.GREEN);
                    wcfFC = new WritableCellFormat(wfont);
                    wcfFC.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
                    wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
                    wcfFC.setAlignment(Alignment.CENTRE);
                    wcfFC.setWrap(true);
                }else{
                    wfont = new jxl.write.WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);
                    wcfFC = new WritableCellFormat(wfont);
                    wcfFC.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
                    wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
                    wcfFC.setAlignment(Alignment.CENTRE);
                    wcfFC.setWrap(true);
                }
                a=a+1;
                String mmPinzhong = mmPinzhongpaixuMap.get("pinzhong").toString();
                int mmPinzhongpiaoshu = 0;
                if (mmPinzhongpaixuMap.get("piaoshu") != null && !"".equals(mmPinzhongpaixuMap.get("piaoshu").toString())) {
                    mmPinzhongpiaoshu = Integer.valueOf(mmPinzhongpaixuMap.get("piaoshu").toString());
                }

                int mmHebingshu = 0;

                List<Map<String, Object>> mmPinguiList = ddService.getAgreedPinguiListByPinzhong(ppxiangmuid, mmPinzhong);
                if (mmPinguiList.size()>0){
                    mmHebingshu = mmPinguiList.size()-1;
                }
                int pg=0;
                for (Map<String, Object> mmPinguiMap : mmPinguiList) {
                    pg=pg+1;
                    int mmPinguipiaoshu = 0;

                    String mmpingui = mmPinguiMap.get("pingui")==null ? "" :mmPinguiMap.get("pingui").toString();
                    String mmyaopinmingcheng=mmPinguiMap.get("yaopinmingcheng")==null ? "" :mmPinguiMap.get("yaopinmingcheng").toString();
                    String mmfenlei=mmPinguiMap.get("fenlei")==null ? "" :mmPinguiMap.get("fenlei").toString();
                    String mmguige=mmPinguiMap.get("guige")==null ? "" :mmPinguiMap.get("guige").toString();
                    String mmdanwei=mmPinguiMap.get("danwei")==null ? "" :mmPinguiMap.get("danwei").toString();
                    String mmjiage=mmPinguiMap.get("jiage")==null ? "" :mmPinguiMap.get("jiage").toString();
                    String mmyibaoleibie=mmPinguiMap.get("yibaoleibie")==null ? "" :mmPinguiMap.get("yibaoleibie").toString();
                    String mmguojiajibenyaowu=mmPinguiMap.get("guojiajibenyaowu")==null ? "" :mmPinguiMap.get("guojiajibenyaowu").toString();
                    String mmjicaiyao=mmPinguiMap.get("jicaiyao")==null ? "" :mmPinguiMap.get("jicaiyao").toString();
                    String mmtongguopingjia=mmPinguiMap.get("tongguopingjia")==null ? "" :mmPinguiMap.get("tongguopingjia").toString();
                    String mmzhidaoyuanze=mmPinguiMap.get("zhidaoyuanze")==null ? "" :mmPinguiMap.get("zhidaoyuanze").toString();
                    String mmdongzongyao=mmPinguiMap.get("dongzongyao")==null ? "" :mmPinguiMap.get("dongzongyao").toString();
                    String mmdongzongjilu2=mmPinguiMap.get("dongzongjilu2")==null ? "" :mmPinguiMap.get("dongzongjilu2").toString();
                    String mmqinhuaiyao=mmPinguiMap.get("qinhuaiyao")==null ? "" :mmPinguiMap.get("qinhuaiyao").toString();
                    String mmqinhuaijilu2=mmPinguiMap.get("qinhuaijilu2")==null ? "" :mmPinguiMap.get("qinhuaijilu2").toString();
                    String mmzhidaoyijian=mmPinguiMap.get("zhidaoyijian")==null ? "" :mmPinguiMap.get("zhidaoyijian").toString();
                    String mmbeizhu=mmPinguiMap.get("beizhu")==null ? "" :mmPinguiMap.get("beizhu").toString();
                    String mmPinzhongpaiming=mmPinguiMap.get("pinzhongpaiming")==null ? "" :mmPinguiMap.get("pinzhongpaiming").toString();
                    String mmPinguipiaoshu1=mmPinguiMap.get("pinguipiaoshu")==null ? "" :mmPinguiMap.get("pinguipiaoshu").toString();

//                    List<Map<String, Object>> mmResultList = ddService.findByxiangmuId(ppxiangmuid);
//
//                    List<Map<String, Object>> mmAgreeList = mmResultList.stream()
//                            .filter(pingui -> pingui.get("pingui").toString()
//                                    .equals(mmPingui) &&
//                                    pingui.get("result").toString().equals("1"))
//                            .collect(Collectors.toList());
//                    if (mmAgreeList != null) {
//                        mmPinguipiaoshu = mmAgreeList.size();
//                    }
                    if(mmPinguiMap.get("piaoshu")==null){
                        mmPinguipiaoshu=0;
                    }else {
                        mmPinguipiaoshu=Integer.valueOf(mmPinguiMap.get("piaoshu").toString());
                    }
                    String mmPinzhongtext = String.valueOf(mmPinzhongpiaoshu) + "/" + String.valueOf(mmUserList.size());
                    String mmPinguitext = String.valueOf(mmPinguipiaoshu) + "/" + String.valueOf(mmUserList.size());
                    String mmJieguotext="";


                    if(mmPinguipiaoshu>=mmTongguopiaoshu){
//                        if(2>=mmPinguiList.size()){
//                            mmJieguotext="通过";
//                        }
//                        if(mmPinguiList.size()>2&&2>=pg){
//                            mmJieguotext="通过";
//                        }
//                        if(mmPinguiList.size()>2&&pg>2){
//                            mmJieguotext="不通过";
//                        }
                        mmJieguotext="通过";
                    }else {
                        mmJieguotext="不通过";
                    }


//                    if(a>48){
//                        mmJieguotext="不通过";
//                    }


                    WritableCellFormat alignStyle = new WritableCellFormat(); //单元格样式
                    alignStyle.setBackground(Colour.GREEN); //背静色

                    wsheet.addCell(new Label(2, mmpinpaiKaishihang,mmpingui,wcfFC));
                    wsheet.addCell(new Label(3, mmpinpaiKaishihang,mmyaopinmingcheng,wcfFC));
                    wsheet.addCell(new Label(4, mmpinpaiKaishihang,mmfenlei,wcfFC));
                    wsheet.addCell(new Label(5, mmpinpaiKaishihang,mmguige,wcfFC));
                    wsheet.addCell(new Label(6, mmpinpaiKaishihang,mmdanwei,wcfFC));
                    wsheet.addCell(new Label(7, mmpinpaiKaishihang,mmyibaoleibie,wcfFC));
                    wsheet.addCell(new Label(8, mmpinpaiKaishihang,mmguojiajibenyaowu,wcfFC));
                    wsheet.addCell(new Label(9, mmpinpaiKaishihang,mmjicaiyao,wcfFC));
                    wsheet.addCell(new Label(10, mmpinpaiKaishihang,mmtongguopingjia,wcfFC));
                    wsheet.addCell(new Label(11, mmpinpaiKaishihang,mmzhidaoyuanze,wcfFC));
                    wsheet.addCell(new Label(12, mmpinpaiKaishihang,mmdongzongyao,wcfFC));
                    wsheet.addCell(new Label(13, mmpinpaiKaishihang,mmqinhuaiyao,wcfFC));
                    wsheet.addCell(new Label(14, mmpinpaiKaishihang,mmdongzongjilu2,wcfFC));
                    wsheet.addCell(new Label(15, mmpinpaiKaishihang,mmqinhuaijilu2,wcfFC));
                    wsheet.addCell(new Label(16, mmpinpaiKaishihang,mmPinzhongpaiming,wcfFC));
                    wsheet.addCell(new Label(17, mmpinpaiKaishihang,mmPinguipiaoshu1,wcfFC));
                    wsheet.addCell(new Label(18, mmpinpaiKaishihang,mmPinzhongtext,wcfFC));
                    wsheet.addCell(new Label(19, mmpinpaiKaishihang,mmPinguitext,wcfFC));
                    if("通过".equals(mmJieguotext)){
                        wsheet.addCell(new Label(20, mmpinpaiKaishihang,mmJieguotext,wcfFC));
                    }else {

                        WritableFont wfont1 = new jxl.write.WritableFont(WritableFont.ARIAL, 12, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, Colour.RED);
                        WritableCellFormat wcfFC1 = new WritableCellFormat(wfont1);
                        wcfFC1.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
                        wcfFC1.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
                        wcfFC1.setAlignment(Alignment.CENTRE);
                        wcfFC1.setWrap(true);

                        wsheet.addCell(new Label(20, mmpinpaiKaishihang,mmJieguotext,wcfFC1));
                    }

                    mmpinpaiKaishihang = mmpinpaiKaishihang+1;

                }
                wsheet.addCell(new Label(0, mmpinzhongKaishihang, String.valueOf(a-1),wcfFC));
                wsheet.mergeCells(0, mmpinzhongKaishihang, 0, mmpinzhongKaishihang+mmHebingshu);
                wsheet.addCell(new Label(1, mmpinzhongKaishihang, mmPinzhong,wcfFC));
                wsheet.mergeCells(1, mmpinzhongKaishihang, 1, mmpinzhongKaishihang+mmHebingshu);
                wsheet.mergeCells(16, mmpinzhongKaishihang, 16, mmpinzhongKaishihang+mmHebingshu);
                wsheet.mergeCells(18, mmpinzhongKaishihang, 18, mmpinzhongKaishihang+mmHebingshu);
                mmpinzhongKaishihang = mmpinzhongKaishihang+mmHebingshu+1;

            }
            //wsheet.setPageSetup(PageOrientation.LANDSCAPE.LANDSCAPE, PaperSize.A4,0.5d,0.5d);
            wbook.write(); // 写入文件
            wbook.close();
        } catch(Exception ex)
        {
            ex.printStackTrace();
        }finally{
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }



    }





}
